home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / prospero / propsero.lha / prospero-beta.4.2e / server / pstart.c < prev   
C/C++ Source or Header  |  1992-02-10  |  3KB  |  140 lines

  1. /*
  2.  * Copyright (c) 1989, 1990, 1991 by the University of Washington
  3.  *
  4.  * For copying and distribution information, please see the file
  5.  * <uw-copyright.h>.
  6.  */
  7.  
  8. #include <uw-copyright.h>
  9.  
  10. #include <stdio.h>
  11. #include <netdb.h>
  12. #include <sys/param.h> 
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #include <pwd.h> 
  16.  
  17. #include <psite.h>
  18. #include <pprot.h>
  19. #include <pmachine.h>
  20.  
  21.  
  22. #ifdef PSRV_ROOT
  23. static char    root[40]      = PSRV_ROOT;
  24. #else
  25. static    char    root[40]      = "";
  26. #endif
  27.  
  28. #ifdef AFTPDIRECTORY
  29. static    char    aftpdir[40]   = AFTPDIRECTORY;
  30. #else
  31. static    char    aftpdir[40]   = "";
  32. #endif
  33.  
  34. #ifdef AFSDIRECTORY
  35. static    char    afsdir[40]    = AFSDIRECTORY;
  36. #else
  37. static    char    afsdir[40]    = "";
  38. #endif
  39.  
  40. static struct sockaddr_in     sin = {AF_INET};
  41.  
  42. main(argc,argv)
  43.     int        argc;
  44.     char    *argv[];
  45.     {
  46.     char            pfsdir[MAXPATHLEN];
  47.     char            pfsdat[MAXPATHLEN];
  48.     char            pfshadow[MAXPATHLEN];
  49.     char            dirsrv_b[MAXPATHLEN];
  50.  
  51.     struct passwd       *pw;
  52.     int            ruid,euid;
  53.  
  54.     int            on = 1;
  55.     struct servent         *sp;
  56.     int            prvport = -1;
  57.     char            prvparg[30];
  58.  
  59.     if (argc > 2) {
  60.         fprintf(stderr,"Usage: %s [full-host-name]\n",argv[0]);
  61.         exit(1);
  62.     }
  63.  
  64.     ruid = getuid();
  65.     euid = geteuid();
  66.  
  67.     /* If root try to bind privileged port before changing uid */
  68.     if((ruid == 0) || (euid == 0)) {
  69.         if ((sp = getservbyname("prospero", "udp")) == 0) 
  70.         sin.sin_port = htons((ushort) PROSPERO_PORT);
  71.         else sin.sin_port = sp->s_port;
  72.  
  73.         if ((prvport = socket(AF_INET, SOCK_DGRAM, 0)) < 0) 
  74.         fprintf(stderr, "pstart: Can't open socket\n");
  75.  
  76.         setsockopt(prvport, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
  77.         
  78.         if (bind(prvport, &sin, S_AD_SZ) < 0) {
  79.         fprintf(stderr, "pstart: Can not bind privileged port\n");
  80.         close(prvport);
  81.         prvport = -1;
  82.         }
  83.     }
  84.  
  85.     /* Find the Prospero UID, and setuid if we are root */
  86.     if((pw = getpwnam(P_USER_ID)) == NULL) {
  87.         fprintf(stderr,"%s: Can't find passwd entry for %s.\n",
  88.             argv[0],P_USER_ID);
  89.         exit(1);
  90.     }
  91.  
  92.       if((ruid != pw->pw_uid) || (euid != pw->pw_uid)) {
  93.         if(setgid(pw->pw_gid)) {
  94.           fprintf(stderr,"%s: Can't set gid.\n",argv[0]);
  95.           exit(1);
  96.           }
  97.  
  98.         if(setuid(pw->pw_uid)) {
  99.         fprintf(stderr,"%s: Can't set uid.\n",argv[0]);
  100.         exit(1);
  101.         }
  102.     }
  103.  
  104.     if(chdir(pw->pw_dir)) {
  105.         fprintf(stderr,"%s: Can't change working directory.\n",argv[0]);
  106.         exit(1);
  107.     }
  108.  
  109. #ifdef P_UNDER_UDIR
  110.     strcpy(pfsdir,pw->pw_dir);
  111.  
  112.     sprintf(pfsdat,"%s/%s",pfsdir,P_STORAGE);
  113.     sprintf(pfshadow,"%s/%s",pfsdir,P_SHADOW);
  114. #else
  115.     strcpy(pfsdat,P_FSTORAGE);
  116.     strcpy(pfshadow,P_FSHADOW);
  117. #endif P_UNDER_UDIR
  118.  
  119. #ifdef AFTPUSER
  120.     /* Find FTP directory - if error, use AFTPDIRECTORY */
  121.     if((pw = getpwnam(AFTPUSER)) != NULL) 
  122.         strcpy(aftpdir,pw->pw_dir);
  123. #endif AFTPUSER
  124.  
  125.     sprintf(dirsrv_b,"%s/dirsrv",P_BINARIES);
  126.     if(prvport >= 0) sprintf(prvparg,"-p%d",prvport);
  127.  
  128.     umask(0);
  129.  
  130.     if(prvport >= 0)
  131.         execl(dirsrv_b,"dirsrv",prvparg,root,pfshadow,pfsdat,
  132.           aftpdir,afsdir,((argc > 1) ? argv[1] : 0), 0);
  133.     else 
  134.         execl(dirsrv_b,"dirsrv",root,pfshadow,pfsdat,
  135.           aftpdir,afsdir,((argc > 1) ? argv[1] : 0), 0);
  136.     
  137.     /* Execl failed */
  138.     exit(1);
  139.     }
  140.